﻿// ==================================================
// Copyright © 2016 杭州伯庸科技 版权所有
//
// 文 件 名：AttributeQuery
// 功能描述：
//
// 创建标识：Yu.Liang(俞 亮) 2018/3/26 11:38:06
//
// 修改标识：
// 修改描述：
// ==================================================

using SuperMap.Data;
using SuperMap.Realspace;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using Shine.Framework;

namespace Shine.Tool
{
    public class AttributeQuery : BaseCommand
    {
        IApplication m_Application = null;
        Scene m_Scene = null;
        Workspace m_Workspace = null;
        private FrmQuery myForm = null;

        public AttributeQuery()
        {
            base.m_caption = "属性查询";
            base.m_name = "AttributeQuery";
            base.m_toolTip = "属性查询";
            base.m_message = "属性查询";
            base.m_category = "tool";
        }

        public override void OnCreate(object hook)
        {
            if (m_Application == null)
            {
                m_Application = hook as IApplication;
                m_Scene = (m_Application as IFramework).SceneControl.Scene;
                m_Workspace = m_Scene.Workspace;
            } 
        }

        public override void OnClick()
        {
            if (m_Scene != null)
            {
                if (m_Scene.FindSelection(true).Count() == 0)
                {
                    MessageBox.Show("请选择一个需要查询的要素!");
                    return;
                }

                if (m_Scene.FindSelection(true).Count() > 1)
                {
                    MessageBox.Show(string.Format("只针对单个要素进行查询!"));
                    return;
                }

                this.LoadForm();

            }
            else
            {
                MessageBox.Show("请先加载三维场景，再激活项目管理面板.");
                return;
            }
        }

        private void LoadForm()
        {
            Selection3D[] selections = this.m_Scene.FindSelection(true);
            Selection3D selection = selections[0];
            Recordset record = selection.ToRecordset();

            if (myForm == null || myForm.IsDisposed)
            {
                myForm = FrmQuery.Instance();
                myForm.Application = m_Application;
                myForm.ShowAttributes(record);
            }

            myForm.Show((m_Application as IFramework).MdiForm);

        }

        public override int Bitmap
        {
            get
            {
                string imgFile = Application.StartupPath + "\\Images\\" + GetType().Name + ".png";

                if (System.IO.File.Exists(imgFile) == false) return 0;

                return new Bitmap(Image.FromFile(imgFile)).GetHbitmap().ToInt32();
            }
        }


        public override bool Enabled
        {
            get { 
                return (m_Application as IFramework).IsMapCtrlactive; 
            }
        }
    }
}
